package com.lxmes.hr.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.hr.domain.NcAttendAbnormal;
import com.lxmes.hr.service.INcAttendAbnormalService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 考勤异常Controller
 *
 * @author lx
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/hr/attAb")
public class NcAttendAbnormalController extends BaseController
{
    @Autowired
    private INcAttendAbnormalService ncAttendAbnormalService;

    /**
     * 查询考勤异常列表
     */
    @PreAuthorize("@ss.hasPermi('hr:attAb:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcAttendAbnormal ncAttendAbnormal)
    {
        startPage();
        List<NcAttendAbnormal> list = ncAttendAbnormalService.selectNcAttendAbnormalList(ncAttendAbnormal);
        return getDataTable(list);
    }

    /**
     * 导出考勤异常列表
     */
    @PreAuthorize("@ss.hasPermi('hr:attAb:export')")
    @Log(title = "考勤异常", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcAttendAbnormal ncAttendAbnormal)
    {
        List<NcAttendAbnormal> list = ncAttendAbnormalService.selectNcAttendAbnormalList(ncAttendAbnormal);
        ExcelUtil<NcAttendAbnormal> util = new ExcelUtil<NcAttendAbnormal>(NcAttendAbnormal.class);
        util.exportExcel(response, list, "考勤异常数据");
    }

    /**
     * 获取考勤异常详细信息
     */
    @PreAuthorize("@ss.hasPermi('hr:attAb:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncAttendAbnormalService.selectNcAttendAbnormalById(id));
    }
    /**
     * 获取考勤异常详细信息
     */
    @PreAuthorize("@ss.hasPermi('hr:attAb:query')")
    @GetMapping(value = "/approvalAttAb")
    public AjaxResult approvalAttAb(NcAttendAbnormal ncAttendAbnormal)
    {
        ncAttendAbnormal.setState("1");
        return toAjax(ncAttendAbnormalService.approvalAttAb(ncAttendAbnormal));
    }

    /**
     * 新增考勤异常
     */
    @PreAuthorize("@ss.hasPermi('hr:attAb:add')")
    @Log(title = "考勤异常", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcAttendAbnormal ncAttendAbnormal)
    {
        return toAjax(ncAttendAbnormalService.insertNcAttendAbnormal(ncAttendAbnormal));
    }

    /**
     * 修改考勤异常
     */
    @PreAuthorize("@ss.hasPermi('hr:attAb:edit')")
    @Log(title = "考勤异常", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcAttendAbnormal ncAttendAbnormal)
    {
        return toAjax(ncAttendAbnormalService.updateNcAttendAbnormal(ncAttendAbnormal));
    }

    /**
     * 删除考勤异常
     */
    @PreAuthorize("@ss.hasPermi('hr:attAb:remove')")
    @Log(title = "考勤异常", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncAttendAbnormalService.deleteNcAttendAbnormalByIds(ids));
    }

    /**
     * 导入考勤异常
     */
    @Log(title = "考勤异常", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcAttendAbnormal> util = new ExcelUtil<>(NcAttendAbnormal.class);
        List<NcAttendAbnormal> ncAttendAbnormalList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncAttendAbnormalService.imporIteminfo(ncAttendAbnormalList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcAttendAbnormal> util = new ExcelUtil<>(NcAttendAbnormal.class);
        util.importTemplateExcel(response,"考勤异常");
    }
}
